package com.hex.ds.hdtp.core.web.script.transfer.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.transfer.dto.request.ScriptConvertSourceListRequestDto;
import com.hex.ds.hdtp.core.app.script.transfer.service.IScriptConvertSourceListService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.converter.IScriptConvertSourceListVoConverter;
import com.hex.ds.hdtp.core.web.script.transfer.vo.query.ScriptConvertSourceListQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Files;

/**
 * @Package: com.hex.ds.hdtp.core.web.transfer.script.controller
 * @ClassName ScriptConvertListController
 * @Description:脚本迁移-脚本转换-web接口
 * @Author: wz.li
 * @Date 2023/2/12 11:29
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "03-脚本迁移：脚本转换前源脚本清单")
@RequestMapping("/api/script/convert/list")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptConvertSourceListController {

    private final IScriptConvertSourceListService scriptConvertListService;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    private final IScriptConvertSourceListVoConverter converter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptConvertListQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：脚本转换清单-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 11:28 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("脚本转换清单-分页查询")
    public ResponseEntity queryListByPage(ScriptConvertSourceListQueryVo scriptConvertSourceListQueryVo, PageVo pageVo) {
        log.debug("进入到【ScriptConvertListController.queryListByPage】方法,参数：{}" + scriptConvertSourceListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        ScriptConvertSourceListRequestDto requestDto = converter.toScriptConvertListRequestDto(scriptConvertSourceListQueryVo);
        return ResponseEntity.ok(scriptConvertListService.queryListByPage(requestDto, pageDto));
    }

    /*
     * @Method: queryListByPageAndSourceFileName <br>
     * @Param: [scriptConvertListQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：查询指定文件的转换历史<br>
     * @Author： wz.li<br>
     * @Date： 2023/5/25 18:16 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/convertHistory"})
    @ApiOperation("脚本转换历史清单-分页查询")
    public ResponseEntity queryListByPageAndSourceFileName(ScriptConvertSourceListQueryVo scriptConvertSourceListQueryVo, PageVo pageVo) {
        log.info("进入到【ScriptConvertListController.queryListByPage】方法,参数：{}" + scriptConvertSourceListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        return ResponseEntity.ok(scriptConvertListService.queryListByPageAndSourceFileName(
                scriptConvertSourceListQueryVo.getPkId(),
                scriptConvertSourceListQueryVo.getSourceFileName(),
                pageDto)
        );
    }

    /*
     * @Method: delete <br>
     * @Param: [pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：删除脚本转换清单<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 11:28 <br>
     * @Version： V2.0.2<br>
     */
    @Log("删除脚本转换清单")
    @DeleteMapping(value = {"/{pkId}"})
    @ApiOperation("删除脚本转换清单")
    public ResponseEntity delete(@PathVariable("pkId") final String pkId) {
        log.info("进入【ScriptConvertListController.delete】方法,参数：" + pkId);
        scriptConvertListService.removeById(pkId);
        return ResponseEntity.ok("删除成功");
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：获取转换清单对象<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 18:36 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/{pkId}"})
    @ApiOperation("获取转换清单对象")
    public ResponseEntity queryById(@PathVariable("pkId") final String pkId) {
        log.debug("进入【ScriptConvertListController.queryById】方法,参数{}", pkId);
        return ResponseEntity.ok(scriptConvertListService.queryById(pkId));
    }

    /*
     * @Method: getContrastFileContent <br>
     * @Param: [crunPkId, histPkId] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：获取指定记录与当前最新记录源文件内容<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/16 16:39 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping(value = {"/{crunPkId}/{histPkId}"})
    @ApiOperation("获取指定的转换历史文件对比内容")
    public ResponseEntity getContrastFileContent(@PathVariable("crunPkId") final String crunPkId,
                                                 @PathVariable("histPkId") final String histPkId) {
        log.info("进入【ScriptConvertListController.getContrastFileContent】方法,参数");
        return ResponseEntity.ok(scriptConvertListService.queryConvertHistoryCompareContent(crunPkId, histPkId));
    }

    /**
     * @Method downloadFile
     * @Param [pkIds]
     * @Return org.springframework.http.ResponseEntity
     * @Description 下载源文件和转换后文件
     * @Author Guo jiajun
     * @Date 2023/5/10 15:51
     * @Version V1.0
     */
    @Log("下载源文件和转换后文件")
    @GetMapping(value = {"/download/file"})
    @ApiOperation("下载源文件和转换后文件")
    public ResponseEntity downloadFile(ScriptConvertSourceListQueryVo scriptConvertSourceListQueryVo) throws Exception {
        log.info("进入【ScriptConvertListController.downloadFile】方法,参数：" + scriptConvertSourceListQueryVo);
        String zipFilePath = scriptConvertListService.downloadFile(converter.toScriptConvertListRequestDto(scriptConvertSourceListQueryVo));
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=已转换脚本源文件及转换后文件.zip")
                .body(bar);
    }

}
